<?php

class pager
{

  protected static $page;
  protected static $limit;
  public static $tri;
  public static $triPager;
  public static $limitPager;
  public static $offsetPager;

  public static function setPage($value, $maxRange)
  {
    /*     * * if the page number is not an int or not within range, assign it to page 1 ** */
    if ($value == false)
    {
      /*       * * no page in GET ** */
      $page = 1;
    }
    elseif (filter_var($value, FILTER_VALIDATE_INT, array("min_range" => 1, "max_range" => $maxRange)) == false)
    {
      self::$page = 1;
      return true;
    }
    else
    {
      self::$page = (int) $value;
    }
  }

  public static function setLimit($value)
  {
    if (Validator::getValidator('entier', $value))
      self::$limit = $value;
    else
      return false;
  }

  public static function getPage()
  {
    return self::$page;
  }

  public static function getLimit()
  {
    return self::$limit;
  }

  public static function getPager($totalRecords, $pageName, $limitPerPage, $params = array(), $stylePage='google', $slice='2')
  {
    //set tri
    self::setOrderBy($params);

    //set page
    $paramPage = (array_key_exists('page', $params)) ? $params['page'] : 1;
    self::setPage($paramPage, $totalRecords);
    self::setLimit($limitPerPage);

    if (self::$triPager == 0)
    {
      $pageName = $pageName . '_page';
    }

    if (self::$triPager == 1)
    {
      $pageName = $pageName . '_page_tri';
    }

    $start = 1;

    /*     * * if we have no results then there is no point in going on ** */
    if ($totalRecords == 0)
    {
      return false;
    }
    else
    {
      /*       * * feed the variables to the pager class ** */
      $pager = self::getPagerData($totalRecords, self::$limit, self::$page);

      /*       * * retrieve the variables from the pager class ** */
      self::$offsetPager = $pager->offset;
      self::$limitPager = $pager->limit;
      $page = $pager->page;

      /*       * * begin the menu ** */
      $htmlPagination = '<div class="pagination">';

      if ($stylePage != 'google')
      {

        /*         * * if this is page 1 there is no previous link ** */
        if ($page != 1)
        {
          $htmlPagination .= '<a href="' . url_for($pageName, array_merge($params, array('page' => ($page - 1)))) . '">◄</a>' . "\n";
        }
        else{
          $htmlPagination .= '<span class="disabled">◄</span>';
        }

        /*         * * loop over the pages ** */
        for ($i = 1; $i <= $pager->numPages; $i++)
        {
          if ($i == $pager->page)
          {
            $htmlPagination .= '<span class="selected">' . $i . '</span>' . "\n";
          }
          else
          {
            $htmlPagination .= '<a href="' . url_for($pageName, array_merge($params, array('page' => $i))) . '">' . $i . '</a>' . "\n";
          }
        }

        /*         * * if we are on the last page, we do not need the NEXT link ** */
        if ($page < $pager->numPages)
        {
          $htmlPagination .= '<a href="' . url_for($pageName, array_merge($params, array('page' => ($page + 1)))) . '">►</a>' . "\n";
        }
        else{
          $htmlPagination .= '<span class="disabled">►</span>';
        }
      }
      else
      {
        if ($page != 1)
        {
          $htmlPagination .= '<a href="' . url_for($pageName, array_merge($params, array('page' => ($page - 1)))) . '">◄</a>' . "\n";
        }
        else{
          $htmlPagination .= '<span class="disabled">◄</span>';
        }

        if (($pager->page + $slice) < $pager->numPages)
        {
          $this_far = $page + $slice;
        }
        else
        {
          $this_far = $pager->numPages;
        }

        if (($start + $pager->page) >= 10 && ($pager->page - 10) > 0)
        {
          $start = $page - 10;
        }

        for ($i = $start; $i <= $this_far; $i++)
        {
          if ($i == $pager->page)
          {
            $htmlPagination .= '<span class="selected">' . $i . '</span>' . "\n";
          }
          else
          {
            $htmlPagination .= '<a href="' . url_for($pageName, array_merge($params, array('page' => $i))) . '">' . $i . '</a></li>' . "\n";
          }
        }

        if (($totalRecords - ($pager->limit * $pager->page)) > 0)
        {
          $htmlPagination .= '<a href="' . url_for($pageName, array_merge($params, array('page' => ($page + 1)))) . '">►</a>' . "\n";
        }
        else{
          $htmlPagination .= '<span class="disabled">►</span>';
        }
      }
      $htmlPagination .= '</div>';

      return $htmlPagination;
    }
  }

  public static function setOrderBy($params)
  {
    if (array_key_exists('champ', $params) && array_key_exists('tri', $params))
    {
      $triValue = '';
      if (strtolower($params['tri']) == 'desc')
      {
        $triValue = 'DESC';
      }
      elseif (strtolower($params['tri']) == 'asc')
      {
        $triValue = 'ASC';
      }

      if (!empty($params['champ']) && !empty($triValue))
      {
        $params['champ'] = str_replace('-', '_', $params['champ']);
        self::$triPager = 1;
        self::$tri = ' ORDER BY ' . $params['champ'] . ' ' . $triValue;
        return true;
      }
      else
      {
        self::$triPager = 0;
        self::$tri = '';
        return false;
      }
    }
  }

  /**
   * @calculate paging inforomation
   *
   * @access public static
   *
   * @param int $num_pages
   *
   * @param int $limit
   *
   * @param $page
   *
   * @return object
   *
   * */
  public static function getPagerData($numPages, $limit, $page)
  {
    /*     * * the number of pages ** */
    $numPages = ceil($numPages / $limit);
    $page = max($page, 1);
    $page = min($page, $numPages);
    /*     * * calculate the offset ** */
    $offset = ($page - 1) * $limit;
    /*     * * a new instance of stdClass ** */
    $ret = new stdClass;

    /*     * * assign the variables to the return class object ** */
    $ret->offset = $offset;
    $ret->limit = $limit;
    $ret->numPages = $numPages;
    $ret->page = $page;
    /*     * * return the object ** */
    return $ret;
  }

}

/* * * end of class ** */